package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelPictureAdapter;
import flc.ast.databinding.ActivityMyAlbumBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class MyAlbumActivity extends BaseAc<ActivityMyAlbumBinding> {

    public static int type;
    private int oldPosition;
    private String selPath = "";
    private SelPictureAdapter pictureAdapter;


    @Override
    protected int onCreate() {
        return R.layout.activity_my_album;
    }

    @Override
    protected void initView() {
        mDataBinding.ivMyAlbumBack.setOnClickListener(v -> onBackPressed());

        //相册图片
        pictureAdapter = new SelPictureAdapter();
        mDataBinding.rvPicList.setLayoutManager(new GridLayoutManager(mContext, 4));
        mDataBinding.rvPicList.setAdapter(pictureAdapter);
        pictureAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        pictureAdapter.getItem(oldPosition).setChecked(false);
        pictureAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        pictureAdapter.getItem(position).setChecked(true);
        pictureAdapter.notifyItemChanged(position);
        selPath = pictureAdapter.getItem(position).getPath();
        if (type == 0) {
            CutPhotoActivity.sBitmap = ImageUtils.getBitmap(selPath);
            startActivity(CutPhotoActivity.class);
        } else if (type == 1) {
            ProfessionalPhotoActivity.sBitmap = ImageUtils.getBitmap(selPath);
            ProfessionalPhotoActivity.type = 1;
            startActivity(ProfessionalPhotoActivity.class);
        }
    }

    @Override
    protected void initData() {
        getPermission();
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_picture_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }
                }).request();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.PHOTO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    pictureAdapter.setList(selectMediaEntities);
                    mDataBinding.rvPicList.setVisibility(View.VISIBLE);
                    mDataBinding.tvMyAlbumNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvPicList.setVisibility(View.GONE);
                    mDataBinding.tvMyAlbumNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}